package com.jianqiao.delivery.midtrans.service.impl;

import com.jianqiao.delivery.delivery.service.DeliveryService;
import com.jianqiao.delivery.midtrans.service.MidtransPayService;
import com.jianqiao.delivery.util.HttpClientUtil;
import com.midtrans.Midtrans;
import com.midtrans.httpclient.SnapApi;
import com.midtrans.httpclient.error.MidtransError;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

/**
 * author: cyx
 * create: 2024/8/7 11:28
 **/
@Service
@Slf4j
public class MidtransPayServiceImpl implements MidtransPayService {

    @Value("${midtrans.refundUrl}")
    private String refundUrl;

    @Value("${midtrans.refundSandboxUrl}")
    private String refundSandboxUrl;

    @Value("${midtrans.serverKey}")
    private String serverKey;

    @Value("${midtrans.serverSandboxKey}")
    private String serverSandboxKey;

    @Autowired
    private DeliveryService deliveryService;

    @Override
    public Map<String, String> create(String idRand , BigDecimal amount) throws MidtransError {
        String totalPrice = String.valueOf(amount);

        //设置沙箱环境
//        Midtrans.serverKey = serverSandboxKey;
//        Midtrans.isProduction = false;

        //设置生产环境
        Midtrans.serverKey = serverKey;
        Midtrans.isProduction = true;

        Map<String, Object> params = new HashMap<>();

        Map<String, String> transactionDetails = new HashMap<>();
        transactionDetails.put("order_id", idRand);
        transactionDetails.put("gross_amount", totalPrice);

        Map<String, String> creditCard = new HashMap<>();
        creditCard.put("secure", "true");

        params.put("transaction_details", transactionDetails);
        params.put("credit_card", creditCard);

        String transactionToken = SnapApi.createTransactionToken(params);
        String transactionRedirectUrl = SnapApi.createTransactionRedirectUrl(params);
        System.out.println(transactionToken);
        System.out.println(transactionRedirectUrl);

        Map<String, String> resultMap = new HashMap<>();
        resultMap.put("transactionToken" , transactionToken);
        resultMap.put("transactionRedirectUrl" , transactionRedirectUrl);
        return resultMap;
    }

    @Override
    public String refund(String orderId) {
        //设置退款url
        String newRefundUrl = refundUrl.replace("order_id", orderId);

        //设置请求头Token
        String unBaseTokenStr = serverKey + ":";
        String baseTokenStr = "Basic " + Base64.getEncoder().encodeToString(unBaseTokenStr.getBytes());

        //设置请求头
        HashMap<String ,String> headMap = new HashMap<>();
        headMap.put("authorization" ,baseTokenStr);

        //设置请求体
        HashMap<String ,String> bodyMap = new HashMap<>();
        bodyMap.put("refund_key" ,"refund-" + orderId);
        bodyMap.put("amount" ,"1");
        bodyMap.put("reason" ,"some reason");
        String response = HttpClientUtil.doPost(newRefundUrl, bodyMap, headMap);
        return response;
    }

    @Override
    public void callBack(String orderId) {
        log.info("支付成功，进行回调");
        deliveryService.paySuccess(orderId);
    }

    @Override
    public void errorCallback() {
        System.out.println("errorCallback被回调了");
    }

    @Override
    public void unfinishCallback() {
        System.out.println("unfinishCallback被回调了");
    }

}
